package at.ac.tuwien.ir.controller;

import at.ac.tuwien.ir.model.IndexSize;
import at.ac.tuwien.ir.model.ProgramFunction;

public class CommandLineReader
{
	private final String CreateIndexLong = "--create-index";
	private final String CreatIndexShort = "-i";
	private final String IndexSizeLong = "--index-size";
	private final String IndexSizeShort = "-s";
	private final String OutputFileLong = "--output";
	private final String OutputFileShort = "-o";
	private final String IndexFileLong = "--index-file";
	private final String IndexFileShort = "-f";
	private final String GenerateSimilarityMeasureLong = "--generate-measure";
	private final String GenerateSimilarityMeasureShort = "-m";
	private final String GZipThresholdLong = "--gzip-threshold";
	private final String GZipThresholdShort = "-g";
	private final String CreateProbabilisticMeasureLong = "--create-probabilistic";
	private final String CreateProbabilisticMeasureShort = "-p";
	
	private String[] m_args;
	
	private ProgramFunction m_function;
	private IndexSize m_indexSize;
	private String m_documentsDirectory;
	private String m_indexFilename;
	private String m_outputDirectory;
	private int m_gzipThreshold = Integer.MAX_VALUE;
	
	public CommandLineReader(String[] _args)
	{
		m_args = _args;
	}
	
	public boolean parseArgs()
	{
		try {
			
			if(m_args[0].compareTo(CreateIndexLong) == 0 || m_args[0].compareTo(CreatIndexShort) == 0) {
				m_function = ProgramFunction.GenerateIndex;
				this.parseIndexFileGeneration(1);
			} else if(m_args[0].compareTo(GenerateSimilarityMeasureLong) == 0 || m_args[0].compareTo(GenerateSimilarityMeasureShort) == 0) {
				m_function = ProgramFunction.ComputeSimilarityMeasure;
				this.parseSimilarityMeasure(1);
			} else if(m_args[0].compareTo(CreateProbabilisticMeasureLong) == 0 || m_args[0].compareTo(CreateProbabilisticMeasureShort) == 0) {
				m_function = ProgramFunction.ComputeProbabilisticMeasure;
				this.parseProbabilisticMeasure(1);
			}
		} catch(Exception e) {
			this.printUsageInformation();
			return false;
		}
		
		return true;
	}
	
	private void parseSimilarityMeasure(int index)
	{
		for(int i = index; i < m_args.length; i++) {
			if(m_args[i].compareTo(IndexFileLong) == 0 || m_args[i].compareTo(IndexFileShort) == 0) {
				m_indexFilename = m_args[++i];
			} else if(m_args[i].compareTo(OutputFileLong) == 0 || m_args[i].compareTo(OutputFileShort) == 0) {
				m_outputDirectory = m_args[++i];
			}
		}
	}
	
	private void parseProbabilisticMeasure(int index)
	{
		for(int i = index; i < m_args.length; i++) {
			if(m_args[i].compareTo(OutputFileLong) == 0 || m_args[i].compareTo(OutputFileShort) == 0) {
				m_outputDirectory = m_args[++i];
			} else {
				m_documentsDirectory = m_args[i];
			}
		}
	}

	private void parseIndexFileGeneration(int index) 
	{
		for(int i = index; i < m_args.length; i++) {
			if(m_args[i].compareTo(IndexSizeLong) == 0 || m_args[i].compareTo(IndexSizeShort) == 0) {
				i++;
				if(m_args[i].compareTo("small") == 0) {
					m_indexSize = IndexSize.small;
				} else if(m_args[i].compareTo("medium") == 0) {
					m_indexSize = IndexSize.medium;
				} else if(m_args[i].compareTo("large") == 0) {
					m_indexSize = IndexSize.large;
				} else if(m_args[i].compareTo("all") == 0) {
					m_indexSize = IndexSize.all;
				}
			} else if(m_args[i].compareTo(OutputFileLong) == 0 || m_args[i].compareTo(OutputFileShort) == 0) {
				m_indexFilename = m_args[++i];
			} else if(m_args[i].compareTo(GZipThresholdLong) == 0 || m_args[i].compareTo(GZipThresholdShort) == 0) {
				m_gzipThreshold = Integer.parseInt(m_args[++i]); 
			} else {
				m_documentsDirectory = m_args[i];
			}
		}
	}
	
	private void printUsageInformation()
	{
		
	}
	
	public IndexSize getIndexSize()
	{
		return m_indexSize;
	}
	
	public String getDocumentsDirectory()
	{
		return m_documentsDirectory;
	}
	
	public String getIndexFilename()
	{
		return m_indexFilename;
	}
	
	public String getOutputDirectory()
	{
		return m_outputDirectory;
	}
	
	public ProgramFunction getProgramFunction()
	{
		return m_function;
	}
	
	public int get_gzipThreshold()
	{
		return m_gzipThreshold;
	}
	
	
}
